﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Scheduler;

namespace Win7RovMobileApp
{
    public partial class CheckingMenu : PhoneApplicationPage
    {
        DateTime? expireDate = null;


        public CheckingMenu()
        {
            InitializeComponent();
        }


        void RenderTimeElapsing(object sender, EventArgs e)
        {
            TimeSpan expireMoment = ((DateTime)expireDate).Subtract(DateTime.Now);
            int months = ((DateTime)expireDate).Month - DateTime.Now.Month + 12 * (((DateTime)expireDate).Year - DateTime.Now.Year); 
            int days = ((DateTime)expireDate).Day - DateTime.Now.Day;

            // update the time
            Month1.AnimateTo(months / 10);
            Month2.AnimateTo(months % 10);

            Day1.AnimateTo(days / 10);
            Day2.AnimateTo(days % 10);

            Hour1.AnimateTo(expireMoment.Hours / 10);
            Hour2.AnimateTo(expireMoment.Hours % 10);

            Minute1.AnimateTo(expireMoment.Minutes / 10);
            Minute2.AnimateTo(expireMoment.Minutes % 10);

            Second1.AnimateTo(expireMoment.Seconds / 10);
            Second2.AnimateTo(expireMoment.Seconds % 10);
        }


        private void CheckCarPlates_Click(object sender, EventArgs e)
        {
            // TODO - Get expire DATE from Database
            // ............
            // ............
            // TEST
            if (String.IsNullOrWhiteSpace(CarPlateTextBox.Text.Trim()))
            {
                MessageBox.Show("Va rugam completati mai intai numarul de inmatriculare!");
                return;
            }

            // new DateTime (year, month, day, hour, minute, sec) 
            expireDate = new DateTime(2012, 10, 22, 22, 40, 0);
            // END TEST

            if (expireDate == null)
            {
                MessageBox.Show("Momentan NU exista o rovigneta pentru numarul de inmatriculare introdus!");
                return;
            }

            CompositionTarget.Rendering += new EventHandler(RenderTimeElapsing);

            // show alarm grid
            AlarmImage.Visibility = AlarmTextBlock.Visibility = System.Windows.Visibility.Visible;
            AlarmTextBlock.Text += " " + CarPlateTextBox.Text.Trim();
            AlarmRemoveTextBlock.Text += " " + CarPlateTextBox.Text.Trim();

            // if there is already an alarm with this name
            if (ScheduledActionService.Find(CarPlateTextBox.Text.Trim()) != null)
            {
                AlarmRemoveImage.Visibility = AlarmRemoveTextBlock.Visibility = System.Windows.Visibility.Visible;
                AlarmImage.Visibility = AlarmTextBlock.Visibility = System.Windows.Visibility.Collapsed;
            }
        }


        private void CancelChecking_Click(object sender, EventArgs e)
        {
            AlarmImage.Visibility = AlarmTextBlock.Visibility = AlarmRemoveImage.Visibility = AlarmRemoveTextBlock.Visibility = System.Windows.Visibility.Collapsed;
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }


        private void SetAlarm_Click(object sender, MouseButtonEventArgs e)
        {
            Alarm alarm = new Alarm(CarPlateTextBox.Text.Trim());
            alarm.BeginTime = DateTime.Now.AddSeconds(5);   //TEST for now... TODO - set "expireDate" minus 1 Day
            alarm.Content = "Rovigneta pentru numarul de inmatriculare " + CarPlateTextBox.Text.Trim() + " va expira maine!";
            alarm.Sound = new Uri("alarm.mp3", UriKind.RelativeOrAbsolute);
            ScheduledActionService.Add(alarm);

            MessageBox.Show("Alarma a fost setata cu succes!");

            AlarmImage.Visibility = AlarmTextBlock.Visibility = System.Windows.Visibility.Collapsed;
            AlarmRemoveImage.Visibility = AlarmRemoveTextBlock.Visibility = System.Windows.Visibility.Visible;
        }


        private void RemoveAlarm_Click(object sender, MouseButtonEventArgs e)
        {
            // alarm name will be exactly the car plate number            
            ScheduledActionService.Remove(CarPlateTextBox.Text.Trim());
            MessageBox.Show("Alarma a fost inlaturata cu succes!");

            AlarmImage.Visibility = AlarmTextBlock.Visibility = System.Windows.Visibility.Visible;
            AlarmRemoveImage.Visibility = AlarmRemoveTextBlock.Visibility = System.Windows.Visibility.Collapsed;
        }

    }
}